using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Hmw;
using net_work.Data.Enum.Hmw;

namespace net_work.Config.DataBaseConfig.Hmw;

/// <summary>
/// 作业实体配置类
/// </summary>
public class HomeworkEntityTypeConfiguration : IEntityTypeConfiguration<Homework>
{
    public void Configure(EntityTypeBuilder<Homework> builder)
    {
        builder.ToTable("hmw_hmw");

        builder.HasKey(h => h.Id);

        // 配置字段名映射
        builder.Property(h => h.Id).HasColumnName("hmw_id");
        builder.Property(h => h.Title).HasColumnName("hmw_title").IsRequired().HasMaxLength(64);
        builder.Property(h => h.Description).HasColumnName("hmw_desc").IsRequired().HasMaxLength(1024);
        builder.Property(h => h.BeginTime).HasColumnName("hmw_begin").IsRequired();
        builder.Property(h => h.Deadline).HasColumnName("hmw_dead").IsRequired();

        // 将 AllowMultipleSubmissions 枚举存储为字符串
        builder.Property(h => h.AllowMultiple)
            .HasColumnName("hmw_allow_mul")
            .IsRequired()
            .HasDefaultValue(AllowMultipleSubmissions.Yes)
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (AllowMultipleSubmissions)Enum.Parse(typeof(AllowMultipleSubmissions), v) // 字符串转枚举
            );

        // 将 HomeworkStatus 枚举存储为字符串
        builder.Property(h => h.Status)
            .HasColumnName("hmw_status")
            .IsRequired()
            .HasDefaultValue(HomeworkStatus.UnRelease)
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (HomeworkStatus)Enum.Parse(typeof(HomeworkStatus), v) // 字符串转枚举
            );

        builder.Property(h => h.ReleaseDateTime).HasColumnName("hmw_rel_datetime");
        builder.Property(h => h.TeachId).HasColumnName("teach_id").IsRequired();

        builder.Property(h => h.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(h => h.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(h => h.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(h => h.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(h => h.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(h => h.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}